package com.longrise.co.ui.carlife.fragment;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.longrise.co.R;
import com.longrise.co.ui.carlife.model.NewsBean;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.commonutils.TimeUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-7-22.
 */

public class NewDetailFragment extends BaseFragment {

    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.notice_title)
    TextView noticeTitle;
    @Bind(R.id.notice_time)
    TextView noticeTime;
    @Bind(R.id.notice_web_view)
    WebView mWebView;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_carlife_news_detail;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView() {
        headTitle.setText("车主头条详情");
        NewsBean.ListBean listBean = (NewsBean.ListBean) getArguments().getSerializable("bean");
        initWebView();
        setText(noticeTitle, listBean.getTitle());
        setText(noticeTime, TimeUtil.getDateYMD(listBean.getPublishtime()));

        if(!StringUtils.isEmpty(listBean.getContent())) {
            mWebView.loadDataWithBaseURL("about:blank", listBean.getContent(), "text/html", "utf-8", null);
//            mWebView.loadData(listBean.getContent(), "text/html", "utf-8");
        }
        /*if (!StringUtils.isEmpty(listBean.getContent())) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                // flags
                // FROM_HTML_MODE_COMPACT：html块元素之间使用一个换行符分隔
                // FROM_HTML_MODE_LEGACY：html块元素之间使用两个换行符分隔
                noticeContent.setText(Html.fromHtml(listBean.getContent(), Html.FROM_HTML_MODE_COMPACT));
            } else {
                noticeContent.setText(Html.fromHtml(listBean.getContent()));
            }
        }

        noticeContent.setMovementMethod(LinkMovementMethod.getInstance());*/
    }

    private void initWebView() {
      /*  WebSettings wSet = mWebView.getSettings();
        wSet.setJavaScriptEnabled(true);*/

        mWebView.clearHistory();
        //扩大比例的缩放
        mWebView.getSettings().setUseWideViewPort(true);
        //设置编码　　
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        //支持javascript
        mWebView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        mWebView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        mWebView.getSettings().setBuiltInZoomControls(true);

        //设置 缓存模式 不用缓存 使用localStorage
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        String absolutePath = getActivity().getApplicationContext().getCacheDir().getAbsolutePath();
        mWebView.getSettings().setAppCachePath(absolutePath);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setAppCacheEnabled(true);

        //自适应屏幕
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setLoadWithOverviewMode(true);
    }

    @OnClick(R.id.head_back)
    public void onViewClicked() {
        _mActivity.onBackPressed();
    }

    public static NewDetailFragment newInstance(NewsBean.ListBean bean) {

        Bundle args = new Bundle();
        args.putSerializable("bean", bean);
        NewDetailFragment fragment = new NewDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
